/**
 * 
 */
package at.ac.tuwien.swa075.telco.hlr.service;

import java.util.List;

import at.ac.tuwien.swa075.telco.hlr.HlrException;
import at.ac.tuwien.swa075.telco.hlr.SubscriberStatus;
import at.ac.tuwien.swa075.telco.hlr.dao.entity.CustomerEntity;
import at.ac.tuwien.swa075.telco.hlr.dao.entity.SubscriptionEntity;
import at.ac.tuwien.swa075.telco.hlr.dao.hibernate.HibernateCustomerDAO;
import at.ac.tuwien.swa075.telco.hlr.dao.hibernate.HibernateSubscriberDAO;

/**
 * @author vovmj01
 * 
 */
public class HomeLocationRegistry implements ICustomer, IRegistry, ILocation {

	/**
	 * Returns the device number associated with the given code and number.
	 * 
	 * @param code
	 *            The prefix code that identifies this telco.
	 * @param number
	 *            The phone number that should be associated with a single
	 *            device
	 * @return The international mobile enquipment idetification number (imei)
	 * @throws HlrException
	 *             A problem occured while resolving the phone number. Possible
	 *             errors are:<br>
	 *             The telco code is not correct. The number is not associated
	 *             with any device. An internal error occured while resolving
	 *             the phone number (e.g. database error, etc.)
	 */
	public String resolveDeviceByNumber(String code, String number) throws HlrException {
		HibernateSubscriberDAO subscriberDAO = new HibernateSubscriberDAO();
		SubscriptionEntity entity = subscriberDAO.findSubscriptionByPhoneNumber(code, number);
		if (entity == null) {
			throw new HlrException("Subscriber not found");
		}
		return entity.getImei();
	}

	public int addCustomer(String firstname, String lastname, String birthDate, String adress, String city, String country) throws HlrException {
		HibernateCustomerDAO customerDAO = new HibernateCustomerDAO();
		List customers = customerDAO.selectCustomers(firstname, lastname, birthDate);
		if (customers != null && customers.size() > 0) {
			throw new HlrException("Customer already registered");
		}
		CustomerEntity customer = new CustomerEntity();
		customer.setAdress(adress);
		customer.setBirthDate(birthDate);
		customer.setCity(city);
		customer.setCountry(country);
		customer.setFirstname(firstname);
		customer.setLastname(lastname);
		int id = customerDAO.insertCustomer(customer);
		return id;
	}

	public void addSubscription(int customerId, String imsi, String imei, String code, String number) throws HlrException {
		HibernateCustomerDAO customerDAO = new HibernateCustomerDAO();
		HibernateSubscriberDAO subscriberDAO = new HibernateSubscriberDAO();
		CustomerEntity customer = customerDAO.findCustomer(customerId);
		SubscriptionEntity subscription = new SubscriptionEntity();
		subscription.setCode(code);
		subscription.setCustomer(customer);
		subscription.setImei(imei);
		subscription.setImsi(imsi);
		subscription.setNumber(number);
		subscription.setStatus(SubscriberStatus.OFFLINE);
		subscriberDAO.insertSubscription(subscription);
	}

	public String resolveSubscriberByNumber(String code, String number) throws HlrException {
		HibernateSubscriberDAO subscriberDAO = new HibernateSubscriberDAO();
		SubscriptionEntity entity = subscriberDAO.findSubscriptionByPhoneNumber(code, number);
		if (entity == null) {
			throw new HlrException("Subscriber not found");
		}
		return entity.getImsi();
	}

	public void updateLocation(String imsi, String location) throws HlrException {
		HibernateSubscriberDAO subscriberDAO = new HibernateSubscriberDAO();
		SubscriptionEntity subscription = subscriberDAO.findSubscriptionByImsi(imsi);
		if (subscription == null) {
			throw new HlrException("Subscriber not found");
		}
		subscription.setLocation(location);
		subscriberDAO.updateSubscription(subscription);
	}

	public void deleteCustomer(int customerId) throws HlrException {
		HibernateCustomerDAO customerDAO = new HibernateCustomerDAO();
		customerDAO.deleteCustomer(customerId);
	}

}
